﻿using Basic.FlowModel.AuditSign;
using Basic.FlowModel.DB;
using Basic.FlowRemoteModel.Application.Model;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.SqlSugar;

namespace Basic.FlowDAL.lmpl
{
    internal class FlowAuditSignLogDAL : BasicDAL<DBFlowAuditSignLog, long>, IFlowAuditSignLogDAL
    {
        public FlowAuditSignLogDAL ( IRepository<DBFlowAuditSignLog> basicDAL ) : base(basicDAL, "audit.sign.log")
        {
        }
        public void Add ( AuditSignFileLog add )
        {
            DateTime now = DateTime.Now;
            DBFlowAuditSignLog[] log = add.Items.ConvertAll(a => new DBFlowAuditSignLog
            {
                Id = IdentityHelper.CreateId(),
                SignEmpId = add.SignEmpId,
                CertFileId = a.CertFileId,
                FileId = add.FileId,
                FileUri = add.FileUri,
                SignImgFileId = a.SignImgFileId,
                SignFileId = add.SignFileId,
                SignItemId = add.SignItemId,
                SignTime = now,
                IsVoid = false,
                LogId = add.LogId,
                SignType = a.SignType
            });
            ISqlQueue<DBFlowAuditSignLog> queue = base._BasicDAL.BeginQueue();
            queue.Insert(log);
            queue.UpdateBy<DBFlowAuditSignFile>(new DBFlowAuditSignFile
            {
                Id = add.SignFileId,
                FileId = add.FileId,
                FileUri = add.FileUri,
                LastEditEmpId = add.SignEmpId,
                LastEditTime = now
            }, new string[]
            {
                "FileId",
                "FileUri",
                "LastEditEmpId",
                "LastEditTime"
            });
            _ = queue.Submit();
        }

        public RestoreSignFile[] GetLastSignFile ( long[] ids, long optItemId )
        {
            RestoreSignFile[] files = this._BasicDAL.Gets<RestoreSignFile>(a => ids.Contains(a.SignFileId) && ( a.IsVoid == false || a.OptItemId == optItemId ));
            RestoreSignFileComparer comparer = new RestoreSignFileComparer();
            return files.OrderByDescending(a => a.SignTime).Distinct(comparer).ToArray();
        }
        public RestoreSignFile[] GetLastSignFile ( long[] ids, long[] removeId )
        {
            RestoreSignFile[] files = this._BasicDAL.Gets<RestoreSignFile>(a => ids.Contains(a.SignFileId) && a.IsVoid == false);
            RestoreSignFileComparer comparer = new RestoreSignFileComparer();
            return files.Where(a => removeId.Contains(a.Id) == false).OrderByDescending(a => a.SignTime).Distinct(comparer).ToArray();
        }
        public void Restore ( SignLogSet obj )
        {
            ISqlQueue<DBFlowAuditSignLog> queue = base._BasicDAL.BeginQueue();
            queue.Update(new DBFlowAuditSignLog
            {
                OptItemId = obj.OptItemId,
                IsVoid = false
            }, new string[]
            {
                "OptItemId",
                "IsVoid"
            }, a => obj.Ids.Contains(a.Id));
            queue.UpdateBy<DBFlowAuditSignFile>(obj.Files.ConvertAll(a => new DBFlowAuditSignFile
            {
                Id = a.SignFileId,
                FileId = a.FileId,
                FileUri = a.FileUri,
                LastEditEmpId = a.SignEmpId,
                LastEditTime = a.SignTime
            }), new string[]
            {
                "FileId",
                "FileUri",
                "LastEditEmpId",
                "LastEditTime"
            });
            _ = queue.Submit();
        }

        public void ToVoid ( SignLogSet obj )
        {
            ISqlQueue<DBFlowAuditSignLog> queue = base._BasicDAL.BeginQueue();
            queue.Update(new DBFlowAuditSignLog
            {
                OptItemId = obj.OptItemId,
                IsVoid = true
            }, new string[]
            {
                "OptItemId",
                "IsVoid"
            }, a => obj.Ids.Contains(a.Id));
            queue.UpdateBy<DBFlowAuditSignFile>(obj.Files.ConvertAll(a => new DBFlowAuditSignFile
            {
                Id = a.SignFileId,
                FileId = a.FileId,
                FileUri = a.FileUri,
                LastEditEmpId = a.SignEmpId,
                LastEditTime = a.SignTime
            }), new string[]
            {
                "FileId",
                "FileUri",
                "LastEditEmpId",
                "LastEditItemId",
                "LastEditTime"
            });
            _ = queue.Submit();
        }
    }
}
